<?php
require_once "../include/global.php";
require_once "../add/TegGenerator.php";
require_once"functions.php";
// $conn = dbconn();



			function MiniTable($param="")
		{
			// $mt0_1=Img('mt0_1.png','class="minit"');
			$mt0_1=Img('mt0_1.png');
			$mt0_2=Img('mt0_2.png');
			$mt0_3=Img('mt0_3.png');
			$mt0_4=Img('mt0_4.png');
			
			return Td($mt0_1,$param.'class="minit"').Td($mt0_2,$param.'class="minit"').Td($mt0_3,$param.'class="minit"').Td($mt0_4,$param.'class="minit"');
		}
	
	
	//////////////////////////
	function G_MiniTable($gi=0,$param="")
	{
		if($gi==0)return Tr(MiniTable($param=""));
		
		$query="SELECT `projects`.`name` as pname ,`projects`.`id`
			FROM `pr_gr` 
			JOIN `projects` 
			WHERE `pr_gr`.`pr_id` = `projects`.`id` 
			AND `pr_gr`.`gr_id` ='$gi'
			ORDER BY `projects`.`name`";
			
		$pjs_nms=mysql_query($query);
		while($row=mysql_fetch_assoc($pjs_nms))
		{
			$h.=Th($row[pname],'colspan=1');
			$b.=MiniTable();
		}
			return Tr($h).Tr($b);
	}
	
		function GetMiniTable($evolute,$gi)
	{
		if($evolute)return Table(G_MiniTable($gi),'border=0 rules=all summary=1');
			elseif(!$evolute)return Table(G_MiniTable(),'border=0 rules=all summary=0');
				else return 0;
	}
	//////////////////////////////////////////
		
				Class RealGroup
			{
				var $id;
				var $name;
				var $arr_projs=array();//id&name order by name
				var$nump;//num real pr
				
						function RealGroup($gi,$gn)
						{
							$this->id=$gi;
							$this->name=$gn;
						}
			
						
						function fill_arr_projs($pi,$pn)
					{
							array_push($this->arr_projs,new RealProjs($pi,$pn));
							$this->nump++;
								
					}
					
					function Show()
				{
					echo$this->id.sp(4).$this->name.sp(4).$this->nump.br(2);
					foreach($this->arr_projs as $pr)echo$pr->id.sp(4).$pr->name.br();
					echo br(3);			
				}
				
						function get_pline()//prline в данной группе
				{
					foreach($this->arr_projs as $pr)$prline.=','.$pr->id;		
					
					return '0'.$prline;
				}
			}
			
					
					Class RealProjs
			{
				var $id;
				var $name;
						function RealProjs($pi,$pn)
						{
								$this->id=$pi;
								$this->name=$pn;
						}
			}
			
			
			
			Class StData //пока туписто, без наследы
			{
				var $pi;
				
				var $call;
				var $min;
				var $waste;
				var $service;
				var $withoutIVR;
				
						function StData($c=0,$m=0,$w=0,$s=0,$wI=0,$pi=0)
						{
							$this->pi=$pi;
							$this->call=$c;
							$this->min=$m;
							$this->waste=$w;
							$this->service=$s;
							$this->withoutIVR=$wI;
						}
						
					
					function gen_html()
					{
						$servcol=$this->service_color($this->service);
						
						return Td(clear_data($this->call)).Td(clear_data($this->min)).Td(clear_data($this->waste)).Td(
						clear_data($this->service),"class='service' style='background-color:$servcol;'");
					}
				
				function service_color($scontent)
					{
						if($scontent>0)
						{
							$q=$scontent+0;
							if($q<70)
							{
								 $col='#D69696';
							}
							elseif($q<80)
								{
									 $col='#DDE3A1';
								}
								else
								{
									$col='#9DD696';
								}
						}
						else $col="";
						
						return $col;
					}
					
			}
			
			
			
					Class StDateTime //пока туписто, без наследы
			{
				var $date;
				var $time;
				
				var$arr_data=array();
				
				// var$com_data;//общие данные по всей группе в данной строке
				var$com_data=array();//общие данные по всей группе в данной строке
				
			
				
						function StDateTime($d,$t)
						{
								$this->date=$d;
								$this->time=$t;
						}
						
						
					function fill_arr_data($c,$m,$w,$s,$wI,$pi=0)
					{
						array_push($this->arr_data,new StData($c,$m,$w,$s,$wI,$pi));
					}
					
						function count_com_group_data()
					{
							$call=-1;
							$min=-1;
							$waste=-1;
							$service=-1;
							$withoutIVR = -1;
								foreach($this->arr_data as $data)
							{
								// if ($data->pi=='136')
								// if ($this->date=='2011-09-18')
									// echo $data->pi.' '.($data->withoutIVR).'<br>';
								SUM2($call,$data->call);
								SUM2($withoutIVR,$data->withoutIVR);
								SUM2($min,$data->min);
								AVG2($waste,$data->call,$data->waste);
								AVG2($service,$data->withoutIVR,$data->service);
							}
							if($call>=0)
							{
								if($call==0)
								{
									$waste=0;
									$service=0;
								}
								else
								{								
									$waste/=$call;
									if ($withoutIVR>=0)
										$service/=$withoutIVR;
										//echo $service.br();
									// if (round($service)==1)
									// {
										// echo '<br>';
										// echo '$withoutIVR='.$withoutIVR.'<br>';
										// echo '$service='.($service*$withoutIVR).'<br>';
									// }	
								}
							}
							
						array_push($this->com_data,new StData($call,$min,$waste,$service,$withoutIVR));
					}
					
					function gen_html()
					{
						global$trh;
						foreach($this->arr_data as $data)
						{
							$ht.=$data->gen_html();
						}
						$d=$this->date;
						// $t=$this->time;
						$t=change_delimiter($this->time,':','-');
						// return Tr($ht,"date='$d' time='$t' class='hide'");
						return Tr($ht,"date='$d' time='$t' class='date$d time$t' ".$trh);
					}
					
					
						function gen_html_com()
					{
						global$trh;
						foreach($this->com_data as $data)
						{
							$ht.=$data->gen_html();
						}
						$d=$this->date;
						// $t=$this->time;
						$t=change_delimiter($this->time,':','-');
						
						// return Tr($ht,"date='$d' time='$t' class='hide'");
						return Tr($ht,"date='$d' time='$t' class='date$d time$t' ".$trh);
					}
			}
			
					Class StDate
			{
				var $date;
				var $wday;
				var$arr_data=array();//общая для всех часов
				
				// var$com_data;
				var$com_data=array();
				
				var$arr_datetimes=array();
				
						function StDate($d,$wd)
						{
							$this->date=$d;
							$this->wday=$wd;
						}
						
						function fill_arr_datetimes($sdt)
					{
						array_push($this->arr_datetimes,$sdt);
					}
					
						function count_date_data()
					{
						$arr_st_data=array();//будет массив классов StData
						
							foreach($this->arr_datetimes as $dt)
							{
								array_push($arr_st_data,$dt->arr_data);
							}
						
						$this->arr_data=summarize_StData($arr_st_data);
					}
					
					
								function count_com_group_data()
					{
						$call=-1;
						$min=-1;
						$waste=-1;
						$service=-1;
						$withoutIVR = -1;

						foreach($this->arr_data as $data)
							{
								SUM2($call,$data->call);
								SUM2($min,$data->min);
								SUM2($withoutIVR,$data->withoutIVR);
								AVG2($waste,$data->call,$data->waste);
								AVG2($service,$data->withoutIVR,$data->service);
							}
							if($call>=0)
							{
								if($call==0)
								{
									$waste=0;
									$service=0;
								}
								else
								{								
									$waste/=$call;
									if ($withoutIVR>=0)
										$service/=$withoutIVR;
								}
								
							}
						array_push($this->com_data,new StData($call,$min,$waste,$service, $withoutIVR));
					}
					
					
						function gen_html()
					{
						global$trh;
						foreach($this->arr_data as $data)
						{
							$ht.=$data->gen_html();
						}
						$d=$this->date;
						return Tr($ht,"date='$d' class='date date$d' ".$trh);
					}
					
					
						function gen_html_com()
					{
						global$trh;	
						foreach($this->com_data as $data)
						{
							$ht.=$data->gen_html();
						}
						$d=$this->date;
						return Tr($ht,"date='$d' class='date date$d' ".$trh);
					}
					
			}
			
// mysql_close($conn);
?>
 